package com.mazaiting.redeye.module.user.register

import cn.jpush.im.android.api.JMessageClient
import cn.jpush.im.api.BasicCallback
import com.mazaiting.easy.base.bean.BaseBean
import com.mazaiting.easy.base.mvp.IBaseView
import com.mazaiting.easy.base.presenter.BaseNetPresenter
import com.mazaiting.easy.utils.rx.RxScheduler
import com.mazaiting.log.L
import com.mazaiting.redeye.api.UserApi
import com.mazaiting.redeye.base.ApplicationComponentImpl
import com.mazaiting.redeye.base.Config
import com.mazaiting.redeye.base.CustomConsumer
import com.mazaiting.redeye.base.ICancelPresenter
import com.mazaiting.redeye.bean.UserBean
import com.mazaiting.redeye.utils.Rsa
import com.trello.rxlifecycle3.kotlin.bindToLifecycle
import dagger.Component
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer
import javax.inject.Inject

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-06-21
 * @decription 注册联系人
 */
class RegisterContract {
    /**
     * 注册视图接口
     */
    interface View : IBaseView {
        /**
         * 注册成功
         * @param msg 成功信息
         */
        fun registerSuccess(msg: String)

        /**
         * 注册失败
         * @param msg 异常信息
         */
        fun registerError(msg: String)
    }

    /**
     * 注册主持人
     */
    interface Presenter : ICancelPresenter<View> {
        /**
         * 注册
         * @param userBean 用户信息
         */
        fun register(userBean: UserBean)
    }
}

/**
 * 注册主持人
 * @param userApi 用户 api
 */
class RegisterPresenter
@Inject constructor(
    private val userApi: UserApi
) : BaseNetPresenter<RegisterContract.View>(), RegisterContract.Presenter {
    /**
     * 登录 Disposable
     */
    private var mRegisterDispose: Disposable? = null

    override fun register(userBean: UserBean) = if (isNetConnected) {
        // 极光注册用户名
        val jUsername = userBean.username
        // 极光注册密码
        val jPassword = userBean.password
        // 初始化加密工具类
        val rsa = Rsa(getString(Config.KEY_PUBLIC_KEY))
        // 加密密码
        userBean.password = rsa.encrypt(userBean.password)
        view.onShowLoading("注册中...")
        mRegisterDispose = userApi.register(userBean)
            .compose(RxScheduler.applySchedulers())
            .bindToLifecycle(view.lifecycleProvider)
            .subscribe(object : CustomConsumer<BaseBean> {
                override fun onSuccess(result: BaseBean?) {
                    L.d(result.toString())
                    if (null != result) {
                        if (result.retCode == 1) {
                            jRegister(jUsername, jPassword, result)
                        } else {
                            view.onShowFailed(result.msg)
                            view.registerError(result.msg)
                        }
                    } else {
                        view.onShowFailed(result?.msg)
                        view.registerError("注册失败")
                    }
                }

            }, Consumer<Throwable> { e ->
                L.d(e.message)
                e.message?.let { view.registerError(it) }
                view.onShowFailed(e.message)
            })
    } else {
        view.onShowNoNet()
    }

    override fun cancel() {
        mRegisterDispose?.dispose()
    }

    /**
     * IM 注册
     * @param username 用户名
     * @param password 密码
     * @param result 结果
     */
    private fun jRegister(
        username: String,
        password: String,
        result: BaseBean
    ) {
        // 注册
        JMessageClient.register(username, password, object : BasicCallback() {
            override fun gotResult(code: Int, msg: String?) {
                if (code == 0) {
                    // 请求成功
                    view.onShowSuccess()
                    // 注册成功
                    view.registerSuccess(result.msg)
                } else {
                    view.onShowFailed(msg)
                    msg?.let { view.registerError("code: $code -- msg: $msg") }
                }
            }
        })
    }
}

@Component(dependencies = [ApplicationComponentImpl::class])
interface RegisterComponent {
    /**
     * 注入 Activity
     * @param activity 界面
     */
    fun inject(activity: RegisterActivity)
}